package com.pro.controller;

import com.pro.common.GuavaCommon;
import com.pro.pojo.*;
import com.pro.service.CourseService;
import com.pro.utils.GuavaUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private GuavaUtils guavaUtils;

    //课程树
    @RequestMapping(value = "/getCourseTree")
    @ResponseBody
    public JsonData getCourseTree(){
        JsCourseClass aClass = new JsCourseClass();
        aClass.setId(0);
        aClass.setParnetId(-1);
        aClass.setName("选择分类类型");
        //在缓存中得到这个枚举类，枚举类相当于一个map集合，会以键值对的方式去存储数据，枚举类就是键
        Object ifPresent = guavaUtils.getTenCacheBuild().getIfPresent(String.valueOf(GuavaCommon.CourseTree));
        if(ifPresent!=null){
            return ifPresent!=null? JsonData.buildSuccess(ifPresent): JsonData.buildError("服务器出错了");
        }else{
            List<JsCourseClass> list = courseService.getCourseTree();
            list.add(aClass);
            guavaUtils.getTenCacheBuild().put(String.valueOf(GuavaCommon.CourseTree),list);
        }
        List<JsCourseClass> list = courseService.getCourseTree();
        list.add(aClass);
        return list != null ? JsonData.buildSuccess(list) : JsonData.buildError("服务器出错了");
    }



    //添加树节点
    @RequestMapping("/addCourseTreeNode")
    @ResponseBody
    public JsonData addCourseTreeNode(JsCourseClass courseClass){
        //删除缓存
        guavaUtils.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.CourseTree));
        boolean b = courseService.addCourseTreeNode(courseClass);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //删除树节点
    @RequestMapping("/delCourseTreeNode")
    @ResponseBody
    public JsonData delCourseTreeNode(Integer id){
        //删除缓存
        guavaUtils.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.CourseTree));
        boolean b = courseService.delCourseTreeNode(id);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //修改树节点
    @RequestMapping("/editCourseTreeNode")
    @ResponseBody
    public JsonData editCourseTreeNode(String name,Integer id){
        //删除缓存
        guavaUtils.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.CourseTree));
        boolean b = courseService.editCourseTreeNode(name, id);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //点击节点，展示对应类别下课程信息
    @RequestMapping("/getCheckedCourse")
    @ResponseBody
    public JsonData getCheckedCourse(Integer classId){
        List<JsCourse> checkedCourse = courseService.getCheckedCourse(classId);
        return checkedCourse !=null ? JsonData.buildSuccess(checkedCourse) : JsonData.buildError("服务器出错了");
    }

    //获取课程table
    @RequestMapping("/getCourseTable")
    @ResponseBody
    public JsonData getCourseTable(){
        List<JsCourse> courseTable = courseService.getCourseTable();
        return courseTable!=null ? JsonData.buildSuccess(courseTable) : JsonData.buildError("服务器出错了");
    }

    //根据审核记录表审核状态获取table
    @RequestMapping("/getCourseTableByResult")
    @ResponseBody
    public JsonData getCourseTableByResult(Integer result){
        List<JsCourse> courseTableByResult = courseService.getCourseTableByResult(result);
        return courseTableByResult!=null ? JsonData.buildSuccess(courseTableByResult) : JsonData.buildError("服务器出错了");
    }

    //查看所有课程数量
    @RequestMapping("/getCourseCounts")
    @ResponseBody
    public JsonData getCourseCounts(){
        Integer courseCounts = courseService.getCourseCounts();
        return courseCounts != null ? JsonData.buildSuccess(courseCounts) : JsonData.buildError("服务器出错了");
    }

    //查看课程上架状态的数量
    @RequestMapping("/getCourseStatus")
    @ResponseBody
    public JsonData getCourseStatus(Integer courseStatus){
        Integer status = courseService.getCourseStatus(courseStatus);
        return status != null ? JsonData.buildSuccess(status) : JsonData.buildError("服务器出错了");
    }

    //查看课程审核状态的数量
    @RequestMapping("/getCourseCstatus")
    @ResponseBody
    public JsonData getCourseCstatus(Integer result){
        Integer cstatus = courseService.getCourseCstatus(result);
        return cstatus != null ? JsonData.buildSuccess(cstatus) : JsonData.buildError("服务器出错了");
    }

    //根据课程上架的状态查询课程
    @RequestMapping("/getCourseTableByStatus")
    @ResponseBody
    public JsonData getCourseTableByStatus(Integer courseStatus){
        List<JsCourse> courseTableByStatus = courseService.getCourseTableByStatus(courseStatus);
        return courseTableByStatus != null ? JsonData.buildSuccess(courseTableByStatus) : JsonData.buildError("服务器出错了");
    }

    //根据课程审核的状态查询课
    @RequestMapping("/getCourseTableByCstatus")
    @ResponseBody
    public JsonData getCourseTableByCstatus(Integer courseCstatus){
        List<JsCourse> courseTableByCstatus = courseService.getCourseTableByCstatus(courseCstatus);
        return courseTableByCstatus != null ? JsonData.buildSuccess(courseTableByCstatus) : JsonData.buildError("服务器出错了");
    }

    //查询所有课程分类 给课程分类的下拉框赋值
    @RequestMapping("/getCourseClassAll")
    @ResponseBody
    public JsonData getCourseClassAll(){
        List<JsCourseClass> courseClassAll = courseService.getCourseClassAll();
        return courseClassAll != null ? JsonData.buildSuccess(courseClassAll) : JsonData.buildError("服务器出错了");
    }

    //高级检索
    @RequestMapping("/getCourseRetrieval")
    @ResponseBody
    public JsonData getCourseRetrieval(JsCourse course){
        List<JsCourse> courseRetrieval = courseService.getCourseRetrieval(course);
        return courseRetrieval != null ? JsonData.buildSuccess(courseRetrieval) : JsonData.buildError("服务器出错了");
    }

    //课程的模糊查
    @RequestMapping("/getCourseSearch")
    @ResponseBody
    public JsonData getCourseSearch(String  courseName,String name,Integer courseNprice){
        List<JsCourse> courseSearch = courseService.getCourseSearch(courseName, name, courseNprice);
        return courseSearch != null ? JsonData.buildSuccess(courseSearch) : JsonData.buildError("服务器出错了");
    }


    //添加商品(待审核状态)
    @RequestMapping("/addCourse")
    @ResponseBody
    public JsonData addCourse(JsCourse course){
        boolean b = courseService.addCourse(course);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }
    //添加审核表
    @RequestMapping("/addCourseRecord")
    @ResponseBody
    public JsonData addCourseRecord(SyCourseRecord courseRecord){
        boolean b = courseService.addCourseRecord(courseRecord);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }
    //根据课程id查询
    @RequestMapping("/getCourseById")
    @ResponseBody
    public JsonData getCourseById(Integer id){
        JsCourse courseById = courseService.getCourseById(id);
        return courseById != null ? JsonData.buildSuccess(courseById) : JsonData.buildError("服务器出错了");
    }
    //查询所有商品序号
    @RequestMapping("/getCourseIdAll")
    @ResponseBody
    public JsonData getCourseIdAll(){
        List<JsCourse> courseIdAll = courseService.getCourseIdAll();
        return courseIdAll != null ? JsonData.buildSuccess(courseIdAll) : JsonData.buildError("服务器出错了");
    }

    //根据课程id修改课程的上架状态
    @RequestMapping("/updateCourseStatus")
    @ResponseBody
    public JsonData updateCourseStatus(Integer courseStatus, Integer courseId){
        boolean b = courseService.updateCourseStatus(courseStatus, courseId);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //查看、删除、日志、添加、修改
    //修改
    @RequestMapping("/updateCourse")
    @ResponseBody
    public JsonData updateCourse(JsCourse course){
        boolean b = courseService.updateCourse(course);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }


    //根据订单id查询课程详细信息
    @RequestMapping(value = "/getCourseByOrderId")
    @ResponseBody
    public JsonData getCourseByOrderId(Integer orderId){
        JsCourse courseByOrderId = courseService.getCourseByOrderId(orderId);
        return courseByOrderId != null ? JsonData.buildSuccess(courseByOrderId) : JsonData.buildError("服务器出错了");
    }

    //审核页面的模糊查
    @RequestMapping(value = "/getCourseExamineSearch")
    @ResponseBody
    public JsonData getCourseExamineSearch(String  courseName, String name, String trueName){
        List<JsCourse> courseExamineSearch = courseService.getCourseExamineSearch(courseName, name, trueName);
        return courseExamineSearch != null ? JsonData.buildSuccess(courseExamineSearch) : JsonData.buildError("服务器出错了");
    }

    //课程审核
    @RequestMapping(value = "/courseExamine")
    @ResponseBody
    public JsonData courseExamine(Integer implementId,String implementDate,Integer result,String remarks,Integer courseId){
        boolean b = courseService.courseExamine(implementId, implementDate, result, remarks, courseId);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //查询所有课程操作日志
    @RequestMapping(value = "/getCourseOperationLog")
    @ResponseBody
    public JsonData getCourseOperationLog(){
        List<JsLog> courseOperationLog = courseService.getCourseOperationLog();
        return courseOperationLog != null ? JsonData.buildSuccess(courseOperationLog) : JsonData.buildError("服务器出错了");
    }

    //课程操作日志模糊查
    @RequestMapping(value = "/getCourseOperationLogSearch")
    @ResponseBody
    public JsonData getCourseOperationLogSearch(String trueName,String remark,Integer type,String createDate){
        List<JsLog> courseOperationLogSearch = courseService.getCourseOperationLogSearch(trueName, remark, type, createDate);
        return courseOperationLogSearch != null ? JsonData.buildSuccess(courseOperationLogSearch) : JsonData.buildError("服务器出错了");

    }

    //查看商品历史价格
    @RequestMapping(value = "/getCoursePrice")
    @ResponseBody
    public JsonData getCoursePrice(String dictLabel,String dictValue,String createTime){
        List<SyDictData> coursePrice = courseService.getCoursePrice(dictLabel, dictValue, createTime);
        return coursePrice != null ? JsonData.buildSuccess(coursePrice) : JsonData.buildError("服务器出错了");
    }

    //查询个人商品
    @RequestMapping(value = "/getCourseByCreateId")
    @ResponseBody
    public JsonData getCourseByCreateId(Integer createId){
        List<JsCourse> courseByCreateId = courseService.getCourseByCreateId(createId);
        return courseByCreateId != null ? JsonData.buildSuccess(courseByCreateId) : JsonData.buildError("服务器出错了");
    }

    //个人课程的模糊查询
    @RequestMapping(value = "/getCourseByCreateIdSearch")
    @ResponseBody
    public JsonData getCourseByCreateIdSearch(String courseName, String coursePrimary, String courseNprice,Integer createId){
        List<JsCourse> courseByCreateIdSearch = courseService.getCourseByCreateIdSearch(courseName, coursePrimary, courseNprice,createId);
        return courseByCreateIdSearch != null ? JsonData.buildSuccess(courseByCreateIdSearch) : JsonData.buildError("服务器出错了");

    }

    //个人课程的高级检索
    @RequestMapping(value = "/getCourseByCreateIdRetrieval")
    @ResponseBody
    public JsonData getCourseByCreateIdRetrieval(JsCourse course){
        List<JsCourse> courseByCreateIdRetrieval = courseService.getCourseByCreateIdRetrieval(course);
        return courseByCreateIdRetrieval != null ? JsonData.buildSuccess(courseByCreateIdRetrieval) : JsonData.buildError("服务器出错了");
    }


    //删除商品
    @RequestMapping(value = "/delCourseByCourseId")
    @ResponseBody
    public JsonData delCourseByCourseId(Integer courseId){
        boolean b = courseService.delCourseByCourseId(courseId);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //删除商品对应的评论
    @RequestMapping(value = "/delCommentByCommodityId")
    @ResponseBody
    public JsonData delCommentByCommodityId(Integer commodityId){
        boolean b = courseService.delCommentByCommodityId(commodityId);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }


    //删除审核记录
    @RequestMapping(value = "/delCourseRecord")
    @ResponseBody
    public JsonData delCourseRecord(Integer courseId){
        boolean b = courseService.delCourseRecord(courseId);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //查询商品的评论数量
    @RequestMapping(value = "/getCommentCounts")
    @ResponseBody
    public JsonData getCommentCounts(Integer commodityId){
        Integer commentCounts = courseService.getCommentCounts(commodityId);
        return commentCounts != null ? JsonData.buildSuccess(commentCounts) : JsonData.buildError("服务器出错了");
    }


    //查看不同课程对应的评论
    @RequestMapping(value = "/getCommentByCommodityId")
    @ResponseBody
    public JsonData getCommentByCommodityId(Integer CommodityId){
        List<JsComment> commentByCommodityId = courseService.getCommentByCommodityId(CommodityId);
        return commentByCommodityId != null ? JsonData.buildSuccess(commentByCommodityId) : JsonData.buildError("服务器出错了");
    }

    //敏感词过滤 添加评论时


    //添加审核日志
    @RequestMapping(value = "/addCourseauditLog")
    @ResponseBody
    public JsonData addCourseauditLog(JsAuditLog auditLog){
        boolean b = courseService.addCourseauditLog(auditLog);
        return b ? JsonData.buildSuccess(b) : JsonData.buildError("服务器出错了");
    }

    //根据课程id查询审核详情
    @RequestMapping(value = "/getAuditById")
    @ResponseBody
    public JsonData getAuditById(Integer courseId){
        List<JsCourse> auditById = courseService.getAuditById(courseId);
        return auditById != null ? JsonData.buildSuccess(auditById) : JsonData.buildError("服务器出错了");

    }
    //根据课程id查询审核详情
    @RequestMapping(value = "/getAllCourseUpperCount")
    @ResponseBody
    public JsonData getAllCourseUpperCount(){
        int allCourseUpperCount = courseService.getAllCourseUpperCount();
        return allCourseUpperCount != 0 ? JsonData.buildSuccess(allCourseUpperCount) : JsonData.buildError("0");

    }

}
